package id.agung.wonogirimaps.util;

import id.agung.wonogirimaps.entity.Category;
import id.agung.wonogirimaps.entity.Lokasi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * JSONAdapter class class untuk menangani pertukaran data pada webservice
 * 
 * @author pratama
 * 
 */
public class JSONAdapter
{
	private static final String	TAG				= JSONAdapter.class.getName();
	private InputStream			is				= null;
	private JSONObject			jsonObject		= null;
	private String				json			= "";

	private static final String	TAG_LOCATION	= "location";
	private static final String	TAG_CATEGORY	= "category";
	private static final String	TAG_ID			= "id";
	private static final String	TAG_NAMA		= "nama";
	private static final String	TAG_DESC		= "desc";
	private static final String	TAG_LAT			= "lat";
	private static final String	TAG_LNG			= "lng";
	private static final String	TAG_ALAMAT		= "alamat";
	private static final String	TAG_TELP		= "telp";
	private static final String	TAG_GAMBAR		= "gambar";

	public JSONObject getJSONFromURL(String url)
	{
		try
		{
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);

			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			is = httpEntity.getContent();
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		} catch (ClientProtocolException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		try
		{
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);

			StringBuilder sb = new StringBuilder();
			String line = null;

			while ((line = reader.readLine()) != null)
			{
				sb.append(line + "\n");
			}

			is.close();
			json = sb.toString();
		} catch (Exception e)
		{
			Utils.TRACE(TAG, "error buffered reader");
		}

		try
		{
			jsonObject = new JSONObject(json);

		} catch (JSONException e)
		{
			Utils.TRACE(TAG, "Error jsonObject");
		}

		return jsonObject;
	}

	/**
	 * Method untuk mengambil semua category
	 * 
	 * @param obj
	 * @return listCategory
	 */
	public List<Category> getAllCategory(JSONObject obj)
	{
		List<Category> listCategory = new ArrayList<Category>();
		try
		{
			JSONArray jsonArray = obj.getJSONArray(TAG_CATEGORY);
			for (int i = 0; i < jsonArray.length(); i++)
			{
				JSONObject jObj = jsonArray.getJSONObject(i);
				Category cat = new Category();
				cat.setId(jObj.getInt(TAG_ID));
				cat.setName(jObj.getString(TAG_NAMA));
				
				Utils.TRACE(TAG, cat.getName());

				listCategory.add(cat);
			}
		} catch (Exception e)
		{
			Utils.TRACE(TAG, "error ambil category");
		}
		return listCategory;
	}

	/**
	 * Method untuk mengambil semua lokasi
	 * 
	 * @param obj
	 * @return listLokasi
	 */
	public List<Lokasi> getAllLocation(JSONObject obj)
	{
		List<Lokasi> listLokasi = new ArrayList<Lokasi>();
		try
		{
			JSONArray jsonArray = obj.getJSONArray(TAG_LOCATION);
			for (int i = 0; i < jsonArray.length(); i++)
			{
				JSONObject jObj = jsonArray.getJSONObject(i);

				Lokasi lokasi = new Lokasi();
				lokasi.setNama(jObj.getString(TAG_NAMA));
				lokasi.setDeskripsi(jObj.getString(TAG_DESC));
				lokasi.setLat(jObj.getDouble(TAG_LAT));
				lokasi.setLng(jObj.getDouble(TAG_LNG));
				lokasi.setAlamat(jObj.getString(TAG_ALAMAT));
				lokasi.setTelp(jObj.getString(TAG_TELP));
				lokasi.setGambar(jObj.getString(TAG_GAMBAR));

				listLokasi.add(lokasi);
			}
		} catch (Exception e)
		{
			Utils.TRACE(TAG, "Error ambil semua lokasi");
		}
		return listLokasi;
	}

}
